package com.tang.user.controller;


import com.tang.common.uniform.Constant;
import com.tang.common.uniform.Result;
import com.tang.common.utils.GenerateCaptcha;
import com.tang.common.utils.SendMessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;

@RestController
@Slf4j
@Api("验证码模块")
public class CaptchaController {
    //验证码
    @ApiOperation("验证码")
    @SneakyThrows
    @RequestMapping(value = "/captcha",produces = MediaType.IMAGE_JPEG_VALUE)
    public String captcha(@RequestParam("time") String time, HttpSession session) {
        String verifyCode = GenerateCaptcha.generateVerifyCode(4);
        File file = new File("D:\\Java_WorkSpace\\recruit\\recruit-modules\\recruit-modules-user\\src\\main\\resources\\static","captcha.png");
        GenerateCaptcha.outputImage(150,50,file,verifyCode);
        String result = GenerateCaptcha.inputStream2Base64(new FileInputStream(new File("D:\\Java_WorkSpace\\recruit\\recruit-modules\\recruit-modules-user\\src\\main\\resources\\static", "captcha.png")));
        session.setAttribute(Constant.CAPTCHA, verifyCode);
        System.out.println(verifyCode);
        return "data:image/jpg;base64," + result;
    }

    @RequestMapping("/captcha/message")
    public Result captchaMsg(String tel,HttpSession session){
        String random = ""+Math.random();
        String substring = random.substring(random.length()-6);
        System.out.println(substring);
        session.setAttribute("phoneCaptcha",substring);
        SendMessageUtils.sendMessage(tel,"ST_2020101100000008",substring);
        return Result.success("验证码发送成功，请注意接收");
    }


}
